from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from course import models, courseser


# Create your views here.
class HomeCate(APIView):
    def get(self, request):
        data = models.Category.objects.filter(isdel=False, parent=None).order_by('sort')
        ser = courseser.CategorySerializer(data, many=True)
        alldata = ser.data
        for item in alldata:
            item["children"] = item["children"][:3]
        return Response({"msg": "ok", "data": ser.data})


class TestCate(APIView):
    def get(self, request):
        data = models.Category.objects.filter(isdel=False, parent=None).order_by('sort')
        ser = courseser.CateSerializer(data, many=True)
        return Response({"msg": "ok", "data": ser.data})


class HomeChild(APIView):
    def get(self, request):
        # 先获取一级分类id
        parent = request.query_params.get("parent")
        if not parent:
            return Response({"msg": "一级分类id不能为空", "code": 400})
        # 根据一级分类id获取二级分类
        info = models.Category.objects.filter(isdel=False, id=parent).first()
        ser = courseser.CategorySerializer(info)
        return Response({"msg": "ok", "data": ser.data})
